// 引入
import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入路由组件
import Login from '../views/Login'
import Home from '../views/Home'
import Course from '@/views/home/Course.vue'
import Choose from '@/views/home/Choose.vue'
import Score from '@/views/home/Score.vue'
import Password from '@/views/home/Password.vue'
import Monitor from '@/views/home/Monitor.vue'
import Allcourse from '@/views/home/Allcourse.vue'

// 使用插件
Vue.use(VueRouter)

// 路由规则
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: 'course', component: Course },
      { path: 'score', component: Score },
      { path: 'choose', component: Choose },
      { path: 'password', component: Password },
      { path: 'monitor', component: Monitor },
      { path: 'allcourse', component: Allcourse }
    ]
  }
]

// router实例对象
const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/home') {
    if (sessionStorage.getItem('token')) next()
    else next('/login')
  } else {
    next()
  }
})

// 暴露router
export default router
